<template>
  <div class="notebook-container">
    <!-- 标题 -->
    <el-header class="notebook-header">
      <h1 class="title">农事记录本</h1>
    </el-header>

    <!-- 添加新事件的按钮 -->
    <div class="add-event">
      <el-button type="primary" @click="openDialog">添加新事件</el-button>
    </div>

    <!-- 事件列表 -->
    <el-card class="event-list">
      <el-table :data="events" style="width: 100%">
        <el-table-column prop="eventName" label="事件标题" width="700" />
        <el-table-column label="操作" width="200"> <!-- 调整操作列宽度 -->
          <template #default="scope">
            <el-button type="text" @click="viewDetails(scope.row)">查看详情</el-button>
            <el-button type="text" @click="deleteEvent(scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加事件的对话框 -->
    <el-dialog v-model="dialogVisible" title="添加新事件" width="50%">
      <el-form :model="newEvent" label-width="80px">
        <el-form-item label="标题">
          <el-input v-model="newEvent.eventName" placeholder="请输入事件标题" />
        </el-form-item>
        <el-form-item label="内容">
          <el-input
              v-model="newEvent.event"
              type="textarea"
              :rows="4"
              placeholder="请输入事件内容"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addEvent">保存</el-button>
      </template>
    </el-dialog>

    <!-- 查看事件详情的对话框 -->
    <el-dialog v-model="detailDialogVisible" :title="selectedEvent.title" width="80%">
      <p>{{ selectedEvent.event }}</p>
      <template #footer>
        <el-button type="primary" @click="detailDialogVisible = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/util/request";

export default {
  data() {
    return {
      events: [], // 事件列表
      dialogVisible: false, // 添加事件对话框的显示状态
      detailDialogVisible: false, // 查看详情对话框的显示状态
      newEvent: {
        eventName: '',
        event: '',
      },
      selectedEvent: {
        title: '',
        content: '',
      },
    };
  },
  created() {
    this.getAllMsg()
  },
  methods: {
    getAllMsg(){
  request.get("/api/event/all").then(res=>{
   if(res.data.code === '200'){
     this.events = res.data.data
   }
  })
},
    // 打开添加事件对话框
    openDialog() {
      this.newEvent = { title: '', content: '' }; // 清空表单
      this.dialogVisible = true;
    },
    // 添加新事件
    addEvent() {
      request.post("/api/event/edit" , this.newEvent).then(res=>{
        if (res.data.code === '200'){
          console.log("添加成功")
        }
        else {
          this.$message.warning('请填写标题和内容');
        }
      })
    },
    // 查看事件详情
    viewDetails(event) {
      this.selectedEvent = event;
      this.detailDialogVisible = true;
    },
    // 删除事件
    deleteEvent(index) {
      this.events.splice(index, 1);
      this.$message.success('事件已删除');
    },
  },
};
</script>

<style scoped>
.notebook-container {
  width: 1000px;
  font-family: Arial, sans-serif;
  margin: 20px auto;
  padding: 20px;
  border: 1px solid #ebeef5;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  background-color: #ffffff;
}

.notebook-header {
  text-align: center;
  margin-bottom: 20px;
}

.title {
  font-family: 'Georgia', serif;
  font-size: 32px;
  font-weight: bold;
  color: #409eff;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
}

.add-event {
  margin-bottom: 20px;
  text-align: center; /* 按钮居中 */
}

.event-list {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.el-dialog {
  width: 80% !important;
}

/* 响应式布局 */
@media (max-width: 768px) {
  .notebook-container {
    padding: 10px;
  }

  .title {
    font-size: 24px; /* 小屏幕上标题字体更小 */
  }

  .el-dialog {
    width: 90% !important; /* 小屏幕上对话框更宽 */
  }
}
</style>
